/******************************************************************************
 *                       B O M B E R M A N  2 0 1 2
 * 
 * File Name: Powerup.java
 * 
 * COP 4331 Summer 2012 Project
 * 
 * Authors: Trevor Mancuso, Chris Chiarella, Zach Hoyler, Daniel Martin
 * 
 * 
 * Description: This class generates a random powerup.
 * 
 ******************************************************************************/
package bomberman;

import java.awt.Toolkit;
import java.util.Random;

/**
 *
 * @author Trevor
 */
public class Powerup extends Entity {
    
    public static final int VIRUS_DELAY = 500;
    //Enum for the types of powerups
    public enum Type {
        BOMB_POWER(0.30d, Bomberman.Sprites.BOMB_POWER),
        BOMB_COUNT(0.30d, Bomberman.Sprites.BOMB_COUNT),
        REMOTE_CONTROL(0.30d, Bomberman.Sprites.REMOTE),
        VIRUS(0.10d, Bomberman.Sprites.VIRUS);
        
        private double weight;
        private Bomberman.Sprites sprite;
        
        Type(double weight, Bomberman.Sprites sprite)
        {
            this.sprite = sprite;
            this.weight = weight;
        }
        //Set the sprite of the powerup.
        public Bomberman.Sprites getSprite()
        {
            return sprite;
        }
        //Weight some powerups more than others.
        public static Type randomWeighted()
        {
            double total = 0;
            for (Type t : Type.values())
                total += t.weight;
            
            double random = Math.random()*total;
            
            for (Type t : Type.values())
            {
                random -= t.weight;
                if (random <= 0)
                    return t;
            }
            
            // Shouldn't occur
            return null;
        }
    }
    
    Type type;
    Bomberman game;
    //Constructor
    Powerup(int x, int y, Bomberman game)
    {
        this.game = game;
        this.xPos = x;
        this.yPos = y;
        
        type = Type.randomWeighted();     
        setSprite(type.sprite.getSprite());
        //If networked, send a packet to create a powerup client side
        if(game.network && game.host)
        {
            game.handler.sendPowerup(x, y, type);
        }
        
    }
    //Network constuctor
    Powerup(int x, int y, Bomberman game, String type)
    {
        this.game = game;
        this.xPos = x;
        this.yPos = y;
        
        if(type.equals("BOMB_POWER"))
            this.type = Type.BOMB_POWER;
        if(type.equals("BOMB_COUNT"))
            this.type = Type.BOMB_COUNT;
        if(type.equals("REMOTE_CONTROL"))
            this.type = Type.REMOTE_CONTROL;
        if(type.equals("VIRUS"))
            this.type = Type.VIRUS;
        
        setSprite(this.type.sprite.getSprite());
        
    }
    //Collect the powerup and effect the player depending on type.
    public void collect(Bomber player)
    {
        Sound.POWERUP.play();
        
        if (type == Type.BOMB_POWER)
        {
            player.bombPower++;
            System.out.println("Bomb power up!");
        }
        else if (type == Type.BOMB_COUNT)
        {
            player.maxBombs++;
            
            System.out.println("Max bombs up!");
        }
        if (type == Type.REMOTE_CONTROL)
        {
            player.remoteControl = true;
            System.out.println("Remote control enabled!");
            
        }
        if (type == Type.VIRUS)
        {
            player.giveVirus();
            System.out.println("Hit by a virus");
        }
        
        game.theGame.powerupArray[xPos][yPos] = null;
    }
    
}
